package Presentation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import Logic.Movie;

import sun.util.calendar.LocalGregorianCalendar.Date;

public class MovieDetailsPanel extends JPanel{
	
	private int firstCol = 20;
	private int secondCol = 120;
	private int y = 75;
	private int dy = 30;
	private int height = 20;
	private int width1 = 100;
	private int width2 = 200;
	private String name,director,length,language,year,country,primary,secondary,summary;
	private String[] players;
	
	public MovieDetailsPanel(final MainFrame main, Movie movie, String header, boolean editable){
		super();
		setLayout(null);
		setSize(350,550);
		setBorder(new LineBorder(Color.black));
		
		if (movie != null){
			name = movie.getName();
			director = movie.getDirector();
			length = "" + movie.getLength();
			language = movie.getLanguage();
			year = "" + movie.getYear();
			country = movie.getCountry();
			primary = movie.getPrimary();
			secondary = movie.getSecondary();
			summary = movie.getSummary();
			players = movie.getPlayers();
		}
		else {
			primary = "";
			secondary = "";
		}		
		
		JLabel headerL = new MyLabel(header,300,50,25,10);
		headerL.setHorizontalAlignment(SwingConstants.CENTER);
		headerL.setVerticalAlignment(SwingConstants.CENTER);
		headerL.setFont(new Font("Arial",Font.BOLD,20));		
		add(headerL);
		
		JLabel nameL = new MyLabel("Name",width1,height,firstCol,y);
		add(nameL);		
		final JTextField nameT = new MyTextField(name,width2,height,secondCol,y,editable);
		add(nameT);
		y += dy;
		
		JLabel directorL = new MyLabel("Director",width1,height,firstCol,y);
		add(directorL);		
		final JTextField directorT = new MyTextField(director,width2,height,secondCol,y,editable);		
		add(directorT);
		y += dy;
		
		JLabel lengthL = new MyLabel("Length (min)",width1,height,firstCol,y);
		add(lengthL);		
		final JTextField lengthT = new MyTextField(length,width1,height,secondCol,y,editable);
		add(lengthT);
		y += dy;
		
		JLabel languageL = new MyLabel("Language",width1,height,firstCol,y);
		add(languageL);		
		final JTextField languageT = new MyTextField(language,width1,height,secondCol,y,editable);
		add(languageT);	
		y += dy;
		
		JLabel yearL = new MyLabel("Year",width1,height,firstCol,y);
		add(yearL);		
		final JTextField yearT = new MyTextField(year,width1,height,secondCol,y,editable);
		add(yearT);
		y += dy;
		
		JLabel countryL = new MyLabel("Country",width1,height,firstCol,y);
		add(countryL);		
		final JTextField countryT = new MyTextField(country,width1,height,secondCol,y,editable);
		add(countryT);
		y += dy;
		
		String[] genres = {"Comedy","Horror","Drama","Action","Romance",
							"History","Adventure","Family","Crime","Romance",
							"Thriller","Biograpy","Adult","Sci-Fi","Mystery",
							"Controversial","Hard Sex","Musical","Animation",""};
		JLabel primaryL = new MyLabel("Primary Genre",width1,height,firstCol,y);
		add(primaryL);		
		final JComboBox primaryC = new JComboBox(genres);
		primaryC.setSize(width1, height);
		primaryC.setLocation(secondCol, y);
		primaryC.setSelectedItem(primary);
		add(primaryC);
		y += dy;
		
		JLabel secondL = new MyLabel("Secondary Genre",width1,height,firstCol,y);
		add(secondL);		
		final JComboBox secondC = new JComboBox(genres);
		secondC.setSize(width1, height);
		secondC.setLocation(secondCol, y);
		secondC.setSelectedItem(secondary);
		add(secondC);
		y += dy;
		
		JLabel playersL = new MyLabel("Players",width1,height,firstCol,y);
		add(playersL);		
		
		String playersString = "";
		if (players != null){
			for (String s : players)
				playersString += s + ", ";
		}
		final JTextArea playersT = new JTextArea(playersString);
		playersT.setSize(width2,3*height);
		playersT.setLocation(secondCol,y);
		playersT.setFont(playersL.getFont());
		playersT.setLineWrap(true);
		playersT.setBorder(nameT.getBorder());
		playersT.setEditable(editable);
		add(playersT);
		y += 3*height + 10;
		
		JLabel summaryL = new MyLabel("Summary",width1,height,firstCol,y);
		add(summaryL);		
		final JTextArea summaryT = new JTextArea(summary);
		summaryT.setSize(width2,4*height);
		summaryT.setLocation(secondCol,y);
		summaryT.setFont(playersL.getFont());
		summaryT.setLineWrap(true);
		summaryT.setBorder(nameT.getBorder());
		summaryT.setEditable(editable);
		add(summaryT);
		y += 4*height +10;
		
		if (movie == null){
			JButton addB = new MyButton("Add",100,30,125,y);
			add(addB);
			addB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					String[] vals = new String[12];
					vals[0] = nameT.getText(); 
					vals[1] = directorT.getText();
					vals[2] = lengthT.getText();
					vals[3] = languageT.getText();
					vals[4] = yearT.getText();
					vals[5] = countryT.getText();
					vals[6] = (String)primaryC.getSelectedItem();					
					vals[7] = (String)secondC.getSelectedItem();
					vals[8] = playersT.getText();
					vals[9] = summaryT.getText();
					vals[10] = "1";
					vals[11] = "1";
					main.addNewMovie(vals);
				}
			});
		}
		else {
			JButton editB = new MyButton("Edit",100,30,25,y);
			add(editB);
			editB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					
				}
			});
			
			JButton rankB = new MyButton("Rank It!",100,30,150,y);
			add(rankB);
			rankB.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					String[] ranks = {"1", "2", "3","4", "5", "6","7", "8", "9","10"};
					String s = (String)JOptionPane.showInputDialog(
					                    main,
					                    "Choose Your Rank:",					                    
					                    "Rank It!",
					                    JOptionPane.PLAIN_MESSAGE,
					                    null,
					                    ranks,
					                    "5");
					if (s != null){
						int rank = Integer.valueOf(s);					
						main.addRank(rank);
					}
				}
			});
			
		}
			
		
	}

}
